-- npm install -g @vtsls/language-server
-- Thanks: https://github.com/yioneko/vtsls/issues/187
-- see: https://code.visualstudio.com/docs/languages/jsconfig
local ignored_ts_codes = {
    [6133] = true,  -- 'X' is declared but its value is never read
    [1005] = true,  -- ';' expected
    [7044] = true,  -- Parameter 'rec' implicitly has an 'any' type, but a better type may be inferred from usage
    [17004] = true, -- Cannot use JSX unless the '--jsx' flag is provided
    [2339] = true,  -- Property 'current' does not exist on type '() => Element'.
    [2554] = true,  -- Expected 1 arguments, but got 0.
    [1345] = true,  -- An expression of type 'void' cannot be tested for truthiness
    [2739] = true,  -- is missing the following properties
    [2740] = true,  -- is missing the following properties
    [2741] = true,  -- Property 'title' is missing in type
    [2322] = true,  -- Property 'scan' does not exist on type
    [2786] = true,  -- cannot be used as a JSX component.
    [2604] = true,  -- JSX element type 'FeedbackCmp' does not have any construct or call signatures
    [2552] = true,  -- Cannot find name 'Ext'. Did you mean 'Text'?
    [2559] = true,  -- Type '{ children: Element[]; }' has no properties in ...
    [2550] = true,  -- Property 'includes' does not exist on type 'string[]'...
    [2304] = true,  -- Cannot find name 'divo'...
    [2551] = true,  -- Property 'CompanySelectorGrid' does not exist ...
    [2363] = true,  -- The right-hand side of an arithmeti...
}
return {
    filetypes = { "javascript", "javascriptreact","typescript" },
    cmd = { "vtsls", "--stdio" },
    on_init = function(client, _)
        local original_handler = vim.lsp.handlers[vim.lsp.protocol.Methods.textDocument_publishDiagnostics]

        vim.lsp.handlers[vim.lsp.protocol.Methods.textDocument_publishDiagnostics] = function(
            err,
            result,
            ctx,
            config
        )
            if result then
                local filtered = vim.tbl_filter(function(d)
                    return d.source ~= "ts" or not ignored_ts_codes[d.code]
                end, result.diagnostics)
                result.diagnostics = filtered
            end
            return original_handler(err, result, ctx, config)
        end
    end
}
-- EOP
